<?php

class DateTimeSelect extends PageElement
{
	private $aDateTime;
	
	private $yearRangeStart=2000;
	private $yearRangeEnd=2020;

	public function __Construct()
	{
		$this->aDateTime = new DateTime("now");
	}

	public function setDateTime($datetime)
	{
		$this->aDateTime = new DateTime($datetime);
	}
	
	public function setYearRangeStart($aYear)
	{
		$this->yearRangeStart=$aYear;
	}

	public function setYearRangeEnd($aYear)
	{
		$this->yearRangeEnd=$aYear;
	}
	
	public function getYearRangeStart()
	{
		return $this->yearRangeStart;
	}

	public function getYearRangeEnd()
	{
		return $this->yearRangeEnd;
	}

	public function getHTML()
	{
	$container = new Hidden();
	$container->setName($this->name);
	$container->setValue($this->aDateTime->format("Y-m-d H:i"));
	$html = $container->getHTML();

	$html .= "<SCRIPT language=\"javascript\">\n";
	$html .= "function ".$this->name."update(unit)\n";
	$html .= "{\n";
	$html .= "document.getElementById(\"".$this->name."\").value = \n";
	$html .= "document.getElementById(\"".$this->name."Year"."\").value + \n";
	$html .= "\"-\" + \n";
	$html .= "document.getElementById(\"".$this->name."Month"."\").value + \n";
	$html .= "\"-\" + \n";
	$html .= "document.getElementById(\"".$this->name."Day"."\").value + \n";
	$html .= "\" \" + \n";
	$html .= "document.getElementById(\"".$this->name."Hour"."\").value + \n";
	$html .= "\":\" + \n";
	$html .= "document.getElementById(\"".$this->name."Minute"."\").value; \n";
//	$html .= "alert(document.getElementById(\"".$this->name."Associate\"));\n";
	$html .= "if(document.getElementById(\"".$this->name."Associate\")!=null)\n";
	$html .= "{\n";
	$html .= $this->name."Associate(unit);\n";
	$html .= "}\n";
	$html .= "}\n";
	$html .= "</SCRIPT>\n";

	$disabled = "";  //????

	// hour
	$html .=  "<select onchange='".$this->name."update(\"Hour\")' name='".$this->name."Hour' id='".$this->name."Hour'".$disabled.">\n";
	for ($i=0; $i<24;$i++)
		{
		$j=$this->aDateTime->format('H');
	//	if($j==-1){$j=23;}

		if(strlen($i)==1){$i = "0".$i;}
		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$j){$html .= " selected ".$j;}
		$html .= ">$i</option>\n";
		}
	$html .= "</select> : ";

	// minute
	$html .= "<select onchange='".$this->name."update(\"Minute\")' name='".$this->name."Minute' id='".$this->name."Minute'".$disabled.">\n";
	for ($i=0; $i<60;$i=$i+5)
		{
		if(strlen($i)==1){$i="0".$i;}
		$html .= "<option value='$i'";
		$cmins = (intval($this->aDateTime->format('i')/5))*5;
		if(ltrim($i,"0")==$cmins){$html .= " selected ".$cmins;}
		$html .= ">$i</option>\n";
		}
	$html .= "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

	// day
	$html .= "<select onchange='".$this->name."update(\"Day\")' name='".$this->name."Day' id='".$this->name."Day'".$disabled.">\n";
	for ($i=1; $i<32;$i++)
		{
		if(strlen($i)==1){$i = "0".$i;}

		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$this->aDateTime->format('d')){$html .= " selected ".$this->aDateTime->format('d');}
		$html .= ">$i</option>\n";
		}
	$html .= "</select> / ";

	// month
	$html .= "<select onchange='".$this->name."update(\"Month\")' name='".$this->name."Month' id='".$this->name."Month'".$disabled.">\n";
	for ($i=1; $i<13;$i++)
		{
		if(strlen($i)==1){$i = "0".$i;}
		
		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$this->aDateTime->format('m')){$html .= " selected ".$this->aDateTime->format('m');}
		$html .= ">$i</option>\n";
		}
	$html .= "</select> / ";

	// year
	$html .= "<select onchange='".$this->name."update(\"Year\")' name='".$this->name."Year' id='".$this->name."Year'".$disabled.">\n";
	for ($i=$this->yearRangeStart; $i<$this->yearRangeEnd;$i++)
		{
		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$this->aDateTime->format('Y')){$html .= " selected ".$this->aDateTime->format('Y');}
		$html .= ">$i</option>\n";
		}
	$html .= "</select>";

	return $html;
	}
}

?>